//
//  HWPushDynamicView.swift
//  HWanGame
//
//  Created by Apple on 2024/8/22.
//  Copyright © 2024 94Wan. All rights reserved.
//

import UIKit
import Stevia

class DynamicFunctionView: UIView {

    // 按钮的宽度和高度
    private let buttonWidth: CGFloat = 61*widthScale
    private let buttonHeight: CGFloat = 26*widthScale
    // 按钮之间的间距
    private let spacing: CGFloat = 12*widthScale
    
    var functionButtonBlock: ((_ funcID: Int) -> Void)?
    
    var imgStr: [String] = [String]() {
        didSet{
            setupButton()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)

        backgroundColor = .clear
    }
    
    private func setupButton(){
        
        // for循环创建按钮
        for i in 0..<imgStr.count {
            // 创建按钮
            let button = UIButton(type: .system)
            // 设置按钮的标题
            let image = imgStr[i].toImage?.withRenderingMode(.alwaysOriginal)
            button.setImage(image, for: .normal)
            // 设置按钮的框架（位置和大小）
            let xPosition = CGFloat(i) * (buttonWidth + spacing)
            button.frame = CGRect(x: xPosition, y: 4, width: buttonWidth, height: buttonHeight)
            // 为按钮添加点击事件
            button.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
            button.tag = i
            // 添加按钮到视图中
            self.addSubview(button)
        }
    }
    
    @objc func buttonTapped(_ sender: UIButton) {
        // 按钮点击事件的处理
        functionButtonBlock?(sender.tag)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

//MARK: 搜索view
class DynamicSearchView: UIView, UITableViewDelegate, UITableViewDataSource {

    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = "#EDEEF0".toColor
        tableView.showsVerticalScrollIndicator = false
        tableView.register(HWDynamicGameCell.self)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.separatorStyle = .none
        tableView.rowHeight = 45*widthScale
        tableView.layer.cornerRadius = 8*widthScale
        tableView.layer.masksToBounds = true
        return tableView
    }()
    
    var dataModel: [HWDynamicGameModel]? {
        didSet{
            tableView.reloadData()
        }
    }
    
    var searchBlock: ((_ model: HWDynamicGameModel) -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)

        backgroundColor = .clear
        
        subviews{
            tableView
        }
        
        tableView.fillContainer()
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return dataModel?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "HWDynamicGameCell", for: indexPath) as! HWDynamicGameCell
        cell.model = dataModel?[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        if let md = dataModel?[indexPath.row] {
            searchBlock?(md)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


//MARK: 话题或者游戏cell
class HWDynamicGameCell: UITableViewCell {
    
    private let tabImageV = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).end()
    private let titleLabel = c_make_ex(UILabel.self)
        .color("#2E3033").font(12.font_medium).end()
    private let subLabel = c_make_ex(UILabel.self)
        .color("#959699").font(10.font_medium).end()

    var model:HWDynamicGameModel? {
        didSet{
            guard let md = model else { return }
            
            titleLabel.text = md.name
            if md.type == "1" {
                tabImageV.image = "Dynamic_smallTopic".toImage
                subLabel.text = md.views
            }else {
                tabImageV.image = "Dynamic_smallGame".toImage
                subLabel.text = md.number
            }
        }
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            tabImageV
            titleLabel
            subLabel
        }
        
        tabImageV.leading(16*widthScale).top(10*widthScale).size(12*widthScale)
        titleLabel.trailing(16*widthScale).leading(32*widthScale).top(6*widthScale)
        subLabel.trailing(16*widthScale).leading(32*widthScale)
        subLabel.Top == titleLabel.Bottom + 4*widthScale
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
